const app = getApp()
let host = app.globalData.host
Page({

  /**
   * 页面的初始数据
   */
  data: {
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 3000,
    duration: 500,

    images: [
      'http://p1.music.126.net/RHDhAOnxZqGtjxjK2s49vg==/109951165466303909.jpg',
      'http://p1.music.126.net/nZtHrj-Wj76vGFn-0kBqaw==/109951165466309360.jpg',
      'http://p1.music.126.net/H1HiJsIq3cLPsfaDLtxRjw==/109951165466767053.jpg',
      'http://p1.music.126.net/oR_IMlwHAu1j0EwfoIVDYw==/109951165466745785.jpg',
      'http://p1.music.126.net/0jPrUkboZ-rFbzCArv-8aQ==/109951165466351711.jpg',
      'http://p1.music.126.net/Xs1sJ_Q3Top3Psv3qGVzmw==/109951165466227994.jpg'
    ]
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
    wx.request({
      url: host + '/showAllCropNotes',
      method: "GET",
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },success (res) {
        for (let i = 0; i < res.data.length; i ++) {
          res.data[i].cropDate = 
          parseInt((new Date - new Date(res.data[i].cropDate)) / (1000 * 60 * 60 * 24))
        }
        that.setData({
          cropItems: res.data
        })
      },
      fail (error){
        console.log("失败")
      }
    })
    wx.request({
      url: host + '/showCamera?cameraId=1',
      method: "GET",
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
       },success (res) {
        let accessToken = res.data.cameraAccessToken
        wx.request({
          url: 'https://open.ys7.com/api/lapp/device/list',
          method: "POST",
          data: {
            accessToken: accessToken,
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
           },success (res) {
             console.log(res)
             that.setData({
               cameras: res.data.data
             })
           },
        fail (error){
           console.log("失败")
        }
        })
       },
      fail (error){
       console.log("失败")
      }
    })

    if (app.globalData.employId && app.globalData.employId != '') {
      let employId = app.globalData.employId
        wx.request({
            url: 'http://localhost:8080'+'/showAdmin?openid='+employId,
            method: "GET",
            header: {
              'content-type': 'application/x-www-form-urlencoded' // 默认值
             },success (res) {
              if (res.data.openid != null) {
                that.setData({
                  isAdmin: true
                })
              }
             },
          fail (error){
             console.log("失败")
          }
      })
    } else {
      app.employIdCallback = employId => {
        if (employId != '') {
          wx.request({
            url: 'http://localhost:8080'+'/showAdmin?openid='+employId,
            method: "GET",
            header: {
              'content-type': 'application/x-www-form-urlencoded' // 默认值
             },success (res) {
              if (res.data.openid != null) {
                that.setData({
                  isAdmin: true,
                })
              }
          },
          fail (error){
             console.log("失败")
          }
      })
        }
      }
    }
  }
})